package system

import (
	"context"

	"admin_template/bff/admin/internal/pkg/utils"
	"admin_template/bff/admin/internal/service"

	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

type Service struct {
	*service.Service
	apiRepo     ApiRepo
	userRepo    UserRepo
	companyRepo CompanyRepo
	ruleRepo    RuleRepo
	roleRepo    RoleRepo
	captchaRepo CaptchaRepo
}

func NewSystemService(s *service.Service,
	apiRepo ApiRepo,
	userRepo UserRepo,
	companyRepo CompanyRepo,
	ruleRepo RuleRepo,
	roleRepo RoleRepo,
	captchaRepo CaptchaRepo) *Service {
	return &Service{
		Service:     s,
		apiRepo:     apiRepo,
		userRepo:    userRepo,
		companyRepo: companyRepo,
		ruleRepo:    ruleRepo,
		roleRepo:    roleRepo,
		captchaRepo: captchaRepo,
	}
}

func (s *Service) Init(ctx context.Context,
	c *app.RequestContext) {
	clientIP := c.ClientIP()
	if !utils.IsInternalIP(clientIP) {
		c.JSON(consts.StatusOK, "非法访问")
	}
	err := s.apiRepo.Init(ctx)
	if err != nil {
		msg := "sysApiRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}
	err = s.ruleRepo.Init(ctx)
	if err != nil {
		msg := "sysRuleRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}
	err = s.roleRepo.Init(ctx)
	if err != nil {
		msg := "sysRoleRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}
	err = s.EnforcerRepo.Init(ctx)
	if err != nil {
		msg := "sysEnforcerRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}

	err = s.companyRepo.Init(ctx)
	if err != nil {
		msg := "sysCompanyRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}
	err = s.userRepo.Init(ctx)
	if err != nil {
		msg := "sysUserRepo init err: " + err.Error()
		c.JSON(consts.StatusOK, msg)
	}
	c.JSON(consts.StatusOK, "ok")
}
